package com.albon.tab;

import com.albon.baseball.BaseballMain;
import com.albon.olympic.OlympicMain;
import com.albon.soccer.SoccerMain;
import com.albon.sportsalarm.R;
import com.albon.util.Logg;
import com.albon.util.Shared;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabWidget;

public class SportAlarmTabmanager extends TabActivity implements TabHost.OnTabChangeListener{

	private final String TAG = "SportAlarmTabmanager";
	
	private TabHost tabHost;
	private final String CURRENT_TAB = "current_tab";
	
	private final int SOCCER = 0;
	private final int BASEBALL = 1;
	private final int OLYMPIC = 2;
	
	public Shared pref;
	
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.tab_manager);
		 pref = new Shared(getBaseContext());
		 tabHost = getTabHost();
		 Intent soccer = new Intent(SportAlarmTabmanager.this, SoccerMain.class);	 
		 Intent baseball = new Intent(SportAlarmTabmanager.this, BaseballMain.class);	 
		 //Intent olympic = new Intent(SportAlarmTabmanager.this, OlympicMain.class);
		 
		 addTabHost(soccer, baseball/*, olympic*/);
		 tabHost.setCurrentTab(pref.getInt(CURRENT_TAB, SOCCER));
		 tabHost.setOnTabChangedListener(this);  
	}
	
	
	private void addTabHost(Intent soccer, Intent baseball/*, Intent olympic*/) {
		// TODO Auto-generated method stub
		tabHost.addTab(tabHost.newTabSpec(getText(R.string.soccer).toString())
	            .setIndicator(TabWidget.inflate(this, R.layout.tab_soccer, null))                
	            .setContent(soccer));
		tabHost.addTab(tabHost.newTabSpec(getText(R.string.baseball).toString())
				.setIndicator(TabWidget.inflate(this, R.layout.tab_baseball, null))                
				.setContent(baseball));
		/*tabHost.addTab(tabHost.newTabSpec(getText(R.string.olympic).toString())
				.setIndicator(TabWidget.inflate(this, R.layout.tab_olympic, null))                
				.setContent(olympic));*/
	}
	
	
	@Override
	public void onTabChanged(String tabId) {
		// TODO Auto-generated method stub
		Activity activity = getLocalActivityManager().getActivity(tabId);
		if (activity != null) {
	        Logg.d(TAG, "not null -> onTabChanged : " + tabId);
	            activity.onWindowFocusChanged(true);
	            if(tabId.equals(this.getText(R.string.soccer).toString())){
	            	saveId(SOCCER);
	            }else if(tabId.equals(this.getText(R.string.baseball).toString())){
	            	saveId(BASEBALL);
	            }/*else{
	            	saveId(OLYMPIC);
	            }*/
	 }
	}
	
	private void saveId(int tabId){
		pref.setInt(CURRENT_TAB, tabId);
	}
}
